<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="组件" prop="component">
      <el-input v-model="dataForm.component" placeholder="组件"></el-input>
    </el-form-item>
    <el-form-item label="告警类型" prop="alertType">
      <el-input v-model="dataForm.alertType" placeholder="告警类型"></el-input>
    </el-form-item>
    <el-form-item label="告警编码" prop="alertCode">
      <el-input v-model="dataForm.alertCode" placeholder="告警编码"></el-input>
    </el-form-item>
    <el-form-item label="告警等级" prop="alertLevel">
      <el-input v-model="dataForm.alertLevel" placeholder="告警等级"></el-input>
    </el-form-item>
    <el-form-item label="中间件IP" prop="middleWareIp">
      <el-input v-model="dataForm.middleWareIp" placeholder="中间件IP"></el-input>
    </el-form-item>
    <el-form-item label="故障及ip" prop="vmIp">
      <el-input v-model="dataForm.vmIp" placeholder="故障及ip"></el-input>
    </el-form-item>
    <el-form-item label="告警时间" prop="alertTime">
      <el-input v-model="dataForm.alertTime" placeholder="告警时间"></el-input>
    </el-form-item>
    <el-form-item label="" prop="alertDesc">
      <el-input v-model="dataForm.alertDesc" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          component: '',
          alertType: '',
          alertCode: '',
          alertLevel: '',
          middleWareIp: '',
          vmIp: '',
          alertTime: '',
          alertDesc: ''
        },
        dataRule: {
          component: [
            { required: true, message: '组件不能为空', trigger: 'blur' }
          ],
          alertType: [
            { required: true, message: '告警类型不能为空', trigger: 'blur' }
          ],
          alertCode: [
            { required: true, message: '告警编码不能为空', trigger: 'blur' }
          ],
          alertLevel: [
            { required: true, message: '告警等级不能为空', trigger: 'blur' }
          ],
          middleWareIp: [
            { required: true, message: '中间件IP不能为空', trigger: 'blur' }
          ],
          vmIp: [
            { required: true, message: '故障及ip不能为空', trigger: 'blur' }
          ],
          alertTime: [
            { required: true, message: '告警时间不能为空', trigger: 'blur' }
          ],
          alertDesc: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornEndogenousUrl(`/componentalarm/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.component = data.componentalarm.component
                this.dataForm.alertType = data.componentalarm.alertType
                this.dataForm.alertCode = data.componentalarm.alertCode
                this.dataForm.alertLevel = data.componentalarm.alertLevel
                this.dataForm.middleWareIp = data.componentalarm.middleWareIp
                this.dataForm.vmIp = data.componentalarm.vmIp
                this.dataForm.alertTime = data.componentalarm.alertTime
                this.dataForm.alertDesc = data.componentalarm.alertDesc
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornEndogenousUrl(`/componentalarm/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'component': this.dataForm.component,
                'alertType': this.dataForm.alertType,
                'alertCode': this.dataForm.alertCode,
                'alertLevel': this.dataForm.alertLevel,
                'middleWareIp': this.dataForm.middleWareIp,
                'vmIp': this.dataForm.vmIp,
                'alertTime': this.dataForm.alertTime,
                'alertDesc': this.dataForm.alertDesc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
